return, and/or¶

Review Lab 5¶

NOTES

  • How did the class solve paint_the_box.py?
    • while/if vs while/while
  • How did they solve waterfall.py?
    • event-stream pattern!

return¶

blue_trail.py¶

Move Bit along the blue trail. Stop when the blue stops, or when Bit is blocked in front.


🖌 and¶

In [1]:
print(True and True)
True
In [2]:
print(True and False)
False
In [3]:
print(False and False)
False

blue_trail_and.py¶

🖌 or¶

red_or_green.py¶

If the square is red or green, paint it blue.

🎨 🖌 Complex logic¶

cross_the_pond.py¶

The green squares above blue are lilypads. They don't jump away as Bit passes by.

The red squares above black are flowers. They don't jump either.

The green squares above black is a frog. It will jump if Bit passes by.

Move Bit to the other side of the pond and clear the frogs.

🐸

Freedom¶

to_freedom.py¶

Move Bit to the first space open on both sides.

In [5]:
%%file for_class/to_freedom.py
from byubit import Bit


def found_freedom(bit):
    return bit.left_clear() and bit.right_clear()


@Bit.worlds('freedom')
def go(bit):
    while not found_freedom(bit):
        bit.move()
        
        
if __name__ == '__main__':
    go(Bit.new_bit)
Overwriting for_class/to_freedom.py

When you hear

Move until condition is true

Think

while not condition:
   bit.move()

NOTES

Strategy for composing a complex condition:

  1. Write a function that determines the condition where Bit should end
  2. while not <function>(bit):

Spiral 🧑🏻‍🎨¶

spiral.py¶

As long as Bit can move forward or left, keep going!

NOTES

Draw it out!

What ending condition do we use?

Under what event do we need to turn?

Key Ideas¶

  • return
  • and, or
  • Writing functions for complex logical statements